<?php
/**
 * Create By: PhpStorm
 * User: yuanxuncheng
 * File: GetOrder.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\cart;

use app\api\model\dao\Cart;
use app\api\model\dao\GoodsSpec;
use app\api\model\dao\Goods;
use app\api\model\dao\OrderGoods;
use app\api\model\dao\OrderInfo;
use app\api\model\dao\Region;
use app\api\model\dao\UserAddress;
use app\api\model\dao\UserCurrency;
use app\library\Error;
use app\library\PageBase;
use think\Validate;
use think\Cache;

class GetNotSubmitCart extends PageBase
{

    private $objOrderInfo;
    private $objOrderGoods;
    private $objGoods;
    private $objRegion;
    private $objCart;
    private $objUserAddress;
    private $objGoodsSpec;
    private $objUserCurrency;

    public function __construct()
    {
        parent::__construct();
        $this->objOrderInfo = new OrderInfo();
        $this->objOrderGoods = new OrderGoods();
        $this->objGoods = new Goods();
        $this->objRegion = new Region();
        $this->objCart = new Cart();
        $this->objUserAddress = new UserAddress();
        $this->objGoodsSpec = new GoodsSpec();
        $this->objUserCurrency = new UserCurrency();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'cart_data' => 'require',
        ];
        $msg = [
            'cart_data.require' => '购物车商品信息必填',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $user_id = $this->user_id;
        $address_id = isset($param['address_id']) ? $param['address_id'] : 0;

        $cart_data = $param['cart_data'];
        $cart_data = json_decode($cart_data, true);
        if (empty($cart_data)) {
            return $this->getRes(Error::ERR_SYS);
        }

        $goods_ids = array_map(function ($v) {
            return $v['goods_id'];
        }, $cart_data);

        // 获取商品
        $goods_amount_sum = 0;
        $goods_arr = [];

        // 获取商品信息
        $goods = $this->objGoods->getGoods(implode(',', $goods_ids));
        foreach ($goods as $good) {
            $goods_arr[$good['goods_id']] = $good;
        }

        foreach ($cart_data as &$v) {
            if (isset($goods_arr[$v['goods_id']])) {
                $goods_number_selected = $v['goods_number'];
                $goods_spec_id = $v['goods_spec_id'];

                $v['goods_name'] = $goods_arr[$v['goods_id']]['goods_name'];

                $v['market_price'] = $goods_arr[$v['goods_id']]['market_price'];
                $v['shop_price'] = $goods_arr[$v['goods_id']]['shop_price'];
                //    $goods_amount_sum += $goods_arr[$v['goods_id']]['shop_price'];
                $v['sale_count'] = $goods_arr[$v['goods_id']]['sale_count'];
                // 加上折扣
                $v['discount'] = '' . round($goods_arr[$v['goods_id']]['shop_price'] / $goods_arr[$v['goods_id']]['market_price'], 2) * 10;
                $v['goods_thumb'] = $goods_arr[$v['goods_id']]['goods_thumb'];

                $goods_number = $goods_arr[$v['goods_id']]['goods_number'];
                $goods_spec = $this->objGoodsSpec->getGoodsSpecByid($goods_spec_id);
                $goods_spec_number = $goods_spec[0]['spec_goods_number'];
                $spec_price = $goods_spec[0]['spec_price'];

                $v['spec_name'] = $goods_spec[0]['spec_name'];
                $v['spec_price'] = $goods_spec[0]['spec_price'];

                if ($goods_number_selected > $goods_number
                    || $goods_number_selected > $goods_spec_number
                ) {
                    return $this->getRes(Error::ERROR_CART_GOODS_NUMBER_ERROR);
                }
                $goods_amount_sum += $spec_price * $goods_number_selected;

            }
        }

        // 获取地址信息
        $user_id = $this->user_id;
        $addresss = $this->objUserAddress->getAddressByUserid($user_id);
        if (empty($addresss)) {
            return $this->getRes(Error::ERR_SYS);
        }

        foreach ($addresss as $address) {
            $regionids = [];
            $regionids[] = $address['country'];
            $regionids[] = $address['province'];
            $regionids[] = $address['city'];
            $regionids[] = $address['district'];

            // 填补地址信息
            $regions = $this->objRegion->getRegion(implode(',', $regionids));
            if (!empty($regions)) {
                foreach ($regions as $region) {
                    if ($region['region_type'] === 0) {
                        $address['country'] = $region['region_name'];
                    }
                    if ($region['region_type'] === 1) {
                        $address['province'] = $region['region_name'];
                    }
                    if ($region['region_type'] === 2) {
                        $address['city'] = $region['region_name'];
                    }
                    if ($region['region_type'] === 3) {
                        $address['district'] = $region['region_name'];
                    }
                }
            }
        }

        // 获取支付列表
        $payments = [
            [
                'pay_id' => 1,
                'name' => '支付宝',
                'icon' => 'https://ss0.bdstatic.com/94oJfD_bAAcT8t7mm9GUKT-xh_/timg?image&quality=100&size=b4000_4000&sec=1487439692&di=40045f06e39314763cae1c0337867e5c&src=http://n1.itc.cn/img8/wb/recom/2016/06/18/146621846347974559.JPEG',
            ],
            [
                'pay_id' => 2,
                'name' => '微信',
                'icon' => 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1487449785740&di=1e1163b1c7a420b1e6b2123e94f34dbd&imgtype=0&src=http%3A%2F%2Fimg.zcool.cn%2Fcommunity%2F01da9955da9dd46ac7251df86b278e.jpg',
            ],
        ];

        // 配送费

        // 获取漫豆
        $amount = 0;
        $avaliable = 0;
        $cash = 0;
        $conds = array(
            'user_id' => $user_id,
            'currency_type' => ['=', 1],
        );
        $amount = $this->objUserCurrency->getSumAmountByUserid($conds);
        $avaliable = $amount * 0.8;
        $cash = round($avaliable / 100, 2);
        $need_pay = $goods_amount_sum;
        if (!empty($param['is_mandou_open'])) {
            $need_pay = $goods_amount_sum - $cash;
        }

        // 获取漫币


        $this->data = array(
            'address' => $addresss,
            'goods' => $cart_data,
            'payments' => $payments,
            'mandou' => ['total' => $amount, 'avaliable' => $avaliable, 'cash' => $cash],
            'settlement' => [
                'goods_amount_sum' => round($goods_amount_sum, 2),
                'need_pay' => round($need_pay, 2),
                'shipping_fee' => 0,
                'shipping_fee_reduction' => 0,
                'coupon' => 0,
                'mandou' => $cash,
                //    'manbi' => 0,
            ],
        );

        return $this->getRes();
    }
}
